package com.nlp.visualization.service.news.impl;


import com.google.gson.Gson;
import com.hankcs.hanlp.corpus.io.IOUtil;
import com.nlp.visualization.core.seg.ISegmentService;
import com.nlp.visualization.core.seg.SegmentFactory;
import com.nlp.visualization.core.seg.SegmentType;
import com.nlp.visualization.core.seg.filter.SegFilter;
import com.nlp.visualization.pojo.crawler.NewsJson;
import com.nlp.visualization.service.news.INewsService;
import com.nlp.visualization.utils.TimeHelper;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.IOUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author LXM
 * @Title:NewsServiceImpl
 * @Description:
 * @date 2018/3/5上午11:06
 */

@Service
public class NewsServiceImpl implements INewsService {


    //    @Autowired
//    ISegmentService segmentService;
    ISegmentService segmentService = SegmentFactory.getInstance();

    //判断是否都是汉字
    private String IsChineseReg = "[\\u4e00-\\u9fa5]+";

    Logger logger = LoggerFactory.getLogger(NewsServiceImpl.class);

    /**
     * 将新闻分词为纯文本文件
     *
     * @param jsonFile
     * @param textFile
     * @throws IOException
     */
    @Override
    public void newsSeg2Text(File jsonFile, File textFileDir, SegmentType method, SegFilter filter) throws Exception {

        String json;
        //读入文件
        try {
            json = FileUtils.readFileToString(jsonFile);
        } catch (Exception e) {
            logger.error("文件不存在：" + jsonFile.getPath() + "");
            e.printStackTrace();
            return;
        }

        //转对象
        NewsJson news = new Gson().fromJson(json, NewsJson.class);
        StringBuffer buffer = new StringBuffer();
        //分词并保存到指定目录
        List<NewsJson.ItemsBean> items = news.getItems();
        for (NewsJson.ItemsBean item : items) {
            buffer.append(excuseSeg(item.getContent(), method, filter) + "\n");
        }
        String textFileName = TimeHelper.getTime() + "-" + (Math.random() + "") + ".txt";
        File textFile = new File(textFileDir, textFileName);
        FileUtils.writeStringToFile(textFile, buffer.toString(), "UTF-8");
    }

    @Override
    public File newsSeg2Text(String text, File saveDir, SegFilter filter) throws Exception {
        String fileNew = System.currentTimeMillis() + "_" + Math.random() + ".temp";
        File file = new File(saveDir, fileNew);
        String buf = excuseSeg(text, SegmentType.HANLP_CRF, filter);
        FileUtils.writeStringToFile(file, buf, "UTF-8");
        return file;
    }


    /**
     * 将新闻内容分词后，拼接成字符串返回
     *
     * @param content
     * @param method
     * @param filter
     * @return
     */
    private String excuseSeg(String content, SegmentType method, SegFilter filter) {

        String buf = "";
        //执行分词
        List<String> segList = segmentService.executeSegWithNamedEntity(content, filter);
        //过滤和拼接结果
        for (String term : segList) {
            //如果新闻分词后是空的，继续下一个
            if (term.trim().equals(""))
                continue;
            String[] terms = term.split("/");
            try {
                //如果新闻分词后不是汉字，下一个
                if (!terms[0].matches(IsChineseReg))
                    continue;
                if (terms.length > 1) {
                    //词性过滤
//                    if (terms[1].charAt(0) == 'n' || terms[1].charAt(0) == 'a' || terms[1].charAt(0) == 'd' || terms[1].charAt(0) == 'g')
//                        ;
//                    else
//                        continue;
                }
            } catch (Exception e) {
                continue;
            }

            String word = terms[0];
            buf += word + " ";
        }

        return buf;
    }


}
